package cn.explink.dop.service;

import java.util.List;
import java.util.Map;
import java.util.TreeSet;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.explink.dop.dao.OrderDao;
import cn.explink.dop.dao.RestaurantDao;
import cn.explink.dop.dao.StorefrontDao;
import cn.explink.dop.entity.vo.RestaurantVO;

/**
 * 业务层
 *
 * @author firefly 2015-2-5 14:57
 *
 */
@Service
@Transactional
public class RestaurantService {

	@Autowired
	private RestaurantDao restaurantDao;
	@Autowired
	private StorefrontDao storefrontDao;
	@Autowired
	private OrderDao orderDao;

	public String add(final RestaurantVO res) {
		return (String) this.restaurantDao.save(res);
	}

	public void modify(final RestaurantVO res) {
		this.restaurantDao.modify(res);
	}

	public RestaurantVO getById(String restaurantId) {
		return this.restaurantDao.getById(restaurantId);
	}

	public List<RestaurantVO> getListData(final RestaurantVO res) {
		return this.restaurantDao.finds(res);
	}

	public Map<String, Object> getListDataByPage(final RestaurantVO res, final int pageNum, final int pageSize) {
		return this.restaurantDao.findByPage(res, pageNum, pageSize);
	}

	public int deleteData(final List<RestaurantVO> res) {
		/**
		 * 先查询出与餐厅进行关联的店面
		 */
		List<Map<String, Object>> list = this.storefrontDao.findByRestaurantId(res);
		if (list.size() > 0) {
			return 0;
		}
		return this.restaurantDao.delete(res);
	}

	/**
	 *
	 * @Title: checkResName
	 * @Description: 判断餐厅是否已经存在
	 * @author 孟凡岭
	 * @param @param resName
	 * @param @return
	 * @return boolean 返回类型
	 * @throws
	 */
	public boolean checkResName(String resName) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> result = this.restaurantDao.checkResName(resName);
		return result.size() == 0;
	}

	/**
	 * @return
	 *
	 * @Title: stopRows
	 * @Description: 停用店面
	 * @author 孟凡岭
	 * @param @param list
	 * @return void 返回类型
	 * @throws
	 */
	public List<Map<String, Object>> stopRows(List<RestaurantVO> list) {
		// TODO Auto-generated method stub
		TreeSet<String> set = new TreeSet<String>();
		for (RestaurantVO r : list) {
			set.add("'" + r.getId() + "'");
		}
		/**
		 * 拼接餐厅ID
		 */
		String ids = StringUtils.join(set.toArray(), ",");
		/**
		 * 查询出餐厅下属的店面ID
		 */
		List<String> idList = this.storefrontDao.queryStoreIds(ids);
		set.clear();
		for (String id : idList) {
			set.add("'" + id + "'");
		}
		/**
		 * 拼接店面ID
		 */
		String storeIds = StringUtils.join(set.toArray(), ",");
		/**
		 * 通过店面ID和餐厅ID查询出未完成的订单
		 */
		List<Map<String, Object>> countMap = this.orderDao.queryWaitOrderByStoIdAndResId(storeIds, ids);
		/**
		 * 如果size大于0说明有未完成的订单，不进行下面的操作
		 */
		if (countMap.size() > 0) {
			return countMap;
		}
		/**
		 * 更新餐厅状态
		 */
		this.restaurantDao.stopRows(ids);
		/**
		 * 更新店面状态
		 */
		this.storefrontDao.stopByRestaurantId(ids);
		return countMap;

	}

	/**
	 *
	 * @Title: openRows
	 * @Description: 启用餐厅
	 * @author 孟凡岭
	 * @param @param list
	 * @return void 返回类型
	 * @throws
	 */
	public void openRows(List<RestaurantVO> list) {
		// TODO Auto-generated method stub
		TreeSet<String> set = new TreeSet<String>();
		for (RestaurantVO r : list) {
			set.add("'" + r.getId() + "'");
		}
		String ids = StringUtils.join(set.toArray(), ",");
		/**
		 * 更新餐厅状态
		 */
		this.restaurantDao.openRows(ids);
		/**
		 * 更新店面状态
		 */
		this.storefrontDao.openRows(ids);
	}

	public RestaurantDao getRestaurantDao() {
		return this.restaurantDao;
	}

	public void setRestaurantDao(RestaurantDao restaurantDao) {
		this.restaurantDao = restaurantDao;
	}

	public StorefrontDao getStorefrontDao() {
		return this.storefrontDao;
	}

	public void setStorefrontDao(StorefrontDao storefrontDao) {
		this.storefrontDao = storefrontDao;
	}

	public OrderDao getOrderDao() {
		return this.orderDao;
	}

	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}
}
